#!/usr/bin/env python
#-*- coding:utf-8 -*-

# file:reader_regulatorviews.py
# author:ytytyt
# datetime:2021/6/27 16:04
# software: PyCharm
"""
图书管理的蓝图定义
"""
# import module your need
from flask import Blueprint
from flask import render_template, request, redirect
from flask import jsonify
#url_prefix表示要以类似于127.0.0.1/url_prefix/路由的方式进行访问
reader_regulator_bp = Blueprint("reader_regulator", __name__, url_prefix="/reader")  # 初始化蓝图

@reader_regulator_bp.route('/home')
def home():

    return render_template('login.html')

@reader_regulator_bp.route('/register')

def register():

    return render_template('register.html')

#定义添加处理路由
@reader_regulator_bp.route('/add',methods = ['GET','POST'])
def add():
    from controller.readerController import ReaderController
    params = request.args if request.method == 'GET' else request.form
    name = params.get('name')
    password = params.get('password')
    sex = params.get('sex')
    telephone=params.get('telephone')
    kwargs = {
        'name': name,
        'password':password,
        'sex':sex,
        'telephone':telephone

    }
    dict_reader=ReaderController.add(**kwargs)
    return jsonify(dict_reader)
    # return redirect('/todolist/index')
    # return render_template('index.html', todolist=res['data'])
    # return 'this is my web-flask index page!'

# 定义删除数据路由
# @todolist_bp.route('/delete/<AutoID>',methods=['POST','GET','DELETE'])
# def delete(AutoID):
#     from controller.toDolistController import ToDoListController
#     kwargs = {
#         'AutoID':AutoID
#     }
#     ToDoListController.delete(**kwargs)
#     return redirect('/todolist/index')
#
#定义查询方法
@reader_regulator_bp.route('/login',methods=['POST'])
def search():
    from controller.readerController import ReaderController
    params = request.args if request.method == 'GET' else request.form
    print(params)
    name = params.get('name')
    password = params.get('password')
    print(name,password)
    kwargs = {
        'name': name,
        'password' : password
    }

    res = ReaderController.get(**kwargs)
    print(res)
    return jsonify(res)